package com.ybc.chinese.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by ytzyg on 15/1/8.
 */
public class DBManager implements CacheColumns, LogColumns {

    private AtomicInteger mOpenCounter = new AtomicInteger();

    private Context mContext;

    private CacheDatabaseHelper helper;

    private SQLiteDatabase mDb;

    private static DBManager dbManager = null;

    public static synchronized DBManager getInstance(Context context) {
        if (dbManager == null) {
            dbManager = new DBManager(context);
        }
        return dbManager;
    }

    private DBManager(Context context) {
        this.mContext = context;
        helper = new CacheDatabaseHelper(context);
        mDb = helper.getWritableDatabase();
    }

    public synchronized void closeDB() {
        if (mDb != null && mDb.isOpen() && mOpenCounter.decrementAndGet() == 0) {
            mDb.close();
        }
    }

    private void closeCursor(Cursor cursor) {
        if (cursor != null) {
            cursor.close();
        }
    }

    public String getContent(String index) {
        String result = null;
        Cursor cursor = mDb.query(TAB_NAME, null, COLUMN_INDEX + " ='" + index + "'", null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTENT));
        }
        closeCursor(cursor);
        return result;
    }

    public synchronized void setContent(String index, String content) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_INDEX, index);
        values.put(COLUMN_CONTENT, content);
//        long rowID = mDb.insert(TAB_NAME, null, values);
//        if(rowID < 0){
//            mDb.update(TAB_NAME, values, COLUMN_INDEX + " ='" + index + "'", null);
//        }
        mDb.replace(TAB_NAME, null, values);
    }

    public synchronized int delContent(String index) {
        return mDb.delete(TAB_NAME, COLUMN_INDEX + " ='" + index + "'", null);
    }

    public synchronized void setLogContent(long id, String index, String content, int status) {
        ContentValues values = new ContentValues();
        if (id != 0) {
            values.put(COLUMN_ID_LOG, id);
        }
        values.put(COLUMN_INDEX_LOG, index);
        values.put(COLUMN_CONTENT_LOG, content);
        values.put(COLUMN_STATUS_LOG, status);
        mDb.replace(TAB_NAME_LOG, null, values);
    }

    public synchronized int delLogContent(String index) {
        return mDb.delete(TAB_NAME_LOG, COLUMN_INDEX_LOG + " ='" + index + "'", null);
    }

    public synchronized int delLogContents(String[] indexs) {
        return mDb.delete(TAB_NAME_LOG, COLUMN_INDEX_LOG + " =?", indexs);
    }

    public synchronized int delLogContents(int status) {
        return mDb.delete(TAB_NAME_LOG, COLUMN_STATUS_LOG + " =" + status, null);
    }

    public synchronized void delLogContents(String index, int status) {
        String sql = "delete from " + TAB_NAME_LOG + " where " + COLUMN_INDEX_LOG + " like '" + index + "%' " + "and " + COLUMN_STATUS_LOG + " = " + status;
        mDb.execSQL(sql);
    }

    public String getLogContent(String index) {
        String result = null;
        Cursor cursor = mDb.query(TAB_NAME_LOG, null, COLUMN_INDEX_LOG + " ='" + index + "'" + " and " + COLUMN_STATUS_LOG + " = " + 0, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTENT_LOG));
        }
        closeCursor(cursor);
        return result;
    }

    public String getLogContentById(long id) {
        String result = null;
        Cursor cursor = mDb.query(TAB_NAME_LOG, null, COLUMN_ID_LOG + " =" + id, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTENT_LOG));
        }
        closeCursor(cursor);
        return result;
    }

    public long getLogId(String index) {
        long result = 0;
        Cursor cursor = mDb.query(TAB_NAME_LOG, null, COLUMN_INDEX_LOG + " ='" + index + "'" + " and " + COLUMN_STATUS_LOG + " = " + 0, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_ID_LOG));
        }
        closeCursor(cursor);
        return result;
    }

    public String[] getLogContent(String index, int status) {
        String[] result = null;
        Cursor cursor = mDb.query(TAB_NAME_LOG, null, COLUMN_INDEX_LOG + " like '" + index + "%' " + "and " + COLUMN_STATUS_LOG + " = " + status, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            int size = cursor.getCount();
            result = new String[size];
            cursor.moveToFirst();
            for (int i = 0; i < size; i++) {
                result[i] = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTENT_LOG));
            }
        }
        closeCursor(cursor);
        return result;
    }

}
